import { _decorator, AnimationClip, Component, Animation, SpriteFrame } from 'cc';
import { ENTITY_STATE_ENUM, FSM_PARAMS_TYPE_ENUM, PARAMS_NAME_ENUM } from '../../Enums';
import State from '../../Base/State';
import { getInitParamsNumber, getInitParamsTrigger, StateMachine } from '../../Base/StateMachine';
import TurnLeftSubStateMachine from './TurnLeftSubStateMachine';
import IdleSubStateMachine from './IdleSubStateMachine';
import BlockFrontSubStateMachine from './BlockFrontSubStateMachine';
import { EntityManager } from '../../Base/EntityManager';
import BlockTurnLeftSubStateMachine from './BlockTurnLeftSubStateMachine';
import BlockTurnRightSubStateMachine from './BlockTurnRightSubStateMachine';
import TurnRightSubStateMachine from './TurnRightSubStateMachine';
import BlockBackSubStateMachine from './BlockBackSubStateMachine';
import BlockLeftSubStateMachine from './BlockLeftSubStateMachine';
import BlockRightSubStateMachine from './BlockRightSubStateMachine';
import DeathSubStateMachine from './DeathSubStateMachine';
import AttackSubStateMachine from './AttackSubStateMachine';
import AirDeathSubStateMachine from './AirDeathSubStateMachine';
const { ccclass, property } = _decorator;


@ccclass('PlayerStateMachine')
export class PlayerStateMachine extends StateMachine {
    async init() {
        this.playerAnimation = this.addComponent(Animation);
        this.initParams()
        this.initStateMachine()
        this.initAnimationEvent()

        await Promise.all(this.waitingList)
    }

    initParams() {
        this.params.set(PARAMS_NAME_ENUM.IDLE, getInitParamsTrigger())
        this.params.set(PARAMS_NAME_ENUM.TURN_LEFT, getInitParamsTrigger())
        this.params.set(PARAMS_NAME_ENUM.TURN_RIGHT, getInitParamsTrigger())
        this.params.set(PARAMS_NAME_ENUM.BLOCK_FRONT, getInitParamsTrigger())
        this.params.set(PARAMS_NAME_ENUM.BLOCK_BACK, getInitParamsTrigger())
        this.params.set(PARAMS_NAME_ENUM.BLOCK_LEFT, getInitParamsTrigger())
        this.params.set(PARAMS_NAME_ENUM.BLOCK_RIGHT, getInitParamsTrigger())
        this.params.set(PARAMS_NAME_ENUM.BLOCK_TURN_LEFT, getInitParamsTrigger())
        this.params.set(PARAMS_NAME_ENUM.BLOCK_TURN_RIGHT, getInitParamsTrigger())
        this.params.set(PARAMS_NAME_ENUM.DIRECTION, getInitParamsNumber())
        this.params.set(PARAMS_NAME_ENUM.DEATH, getInitParamsTrigger())
        this.params.set(PARAMS_NAME_ENUM.AIR_DEATH, getInitParamsTrigger())
        this.params.set(PARAMS_NAME_ENUM.ATTACK, getInitParamsTrigger())

    }

    initStateMachine() {
        this.stateMachine.set(PARAMS_NAME_ENUM.IDLE, new IdleSubStateMachine(this));
        this.stateMachine.set(PARAMS_NAME_ENUM.TURN_LEFT, new TurnLeftSubStateMachine(this));
        this.stateMachine.set(PARAMS_NAME_ENUM.TURN_RIGHT, new TurnRightSubStateMachine(this));
        this.stateMachine.set(PARAMS_NAME_ENUM.BLOCK_FRONT, new BlockFrontSubStateMachine(this));
        this.stateMachine.set(PARAMS_NAME_ENUM.BLOCK_BACK, new BlockBackSubStateMachine(this));
        this.stateMachine.set(PARAMS_NAME_ENUM.BLOCK_LEFT, new BlockLeftSubStateMachine(this));
        this.stateMachine.set(PARAMS_NAME_ENUM.BLOCK_RIGHT, new BlockRightSubStateMachine(this));
        this.stateMachine.set(PARAMS_NAME_ENUM.BLOCK_TURN_LEFT, new BlockTurnLeftSubStateMachine(this));
        this.stateMachine.set(PARAMS_NAME_ENUM.BLOCK_TURN_RIGHT, new BlockTurnRightSubStateMachine(this));
        this.stateMachine.set(PARAMS_NAME_ENUM.DEATH, new DeathSubStateMachine(this));
        this.stateMachine.set(PARAMS_NAME_ENUM.AIR_DEATH, new AirDeathSubStateMachine(this));
        this.stateMachine.set(PARAMS_NAME_ENUM.ATTACK, new AttackSubStateMachine(this));
    }

    initAnimationEvent() {
        this.playerAnimation.on(Animation.EventType.FINISHED, () => {
            const name = this.playerAnimation.defaultClip.name
            const whiteList = ['block', 'turn', 'attack']
            if (whiteList.some(v => name.includes(v))) {
                this.node.getComponent(EntityManager).state = ENTITY_STATE_ENUM.IDLE
            }
        })

    }


    run() {
        switch (this.currentState) {
            case this.stateMachine.get(PARAMS_NAME_ENUM.TURN_LEFT):
            case this.stateMachine.get(PARAMS_NAME_ENUM.TURN_RIGHT):
            case this.stateMachine.get(PARAMS_NAME_ENUM.BLOCK_FRONT):
            case this.stateMachine.get(PARAMS_NAME_ENUM.BLOCK_BACK):
            case this.stateMachine.get(PARAMS_NAME_ENUM.BLOCK_LEFT):
            case this.stateMachine.get(PARAMS_NAME_ENUM.BLOCK_RIGHT):
            case this.stateMachine.get(PARAMS_NAME_ENUM.BLOCK_TURN_LEFT):
            case this.stateMachine.get(PARAMS_NAME_ENUM.BLOCK_TURN_RIGHT):
            case this.stateMachine.get(PARAMS_NAME_ENUM.ATTACK):
            case this.stateMachine.get(PARAMS_NAME_ENUM.DEATH):
            case this.stateMachine.get(PARAMS_NAME_ENUM.AIR_DEATH):
            case this.stateMachine.get(PARAMS_NAME_ENUM.IDLE):
                if (this.params.get(PARAMS_NAME_ENUM.TURN_LEFT).value) {
                    this.currentState = this.stateMachine.get(PARAMS_NAME_ENUM.TURN_LEFT)
                } else if (this.params.get(PARAMS_NAME_ENUM.TURN_RIGHT).value) {
                    this.currentState = this.stateMachine.get(PARAMS_NAME_ENUM.TURN_RIGHT)
                } else if (this.params.get(PARAMS_NAME_ENUM.BLOCK_FRONT).value) {
                    this.currentState = this.stateMachine.get(PARAMS_NAME_ENUM.BLOCK_FRONT)
                } else if (this.params.get(PARAMS_NAME_ENUM.BLOCK_BACK).value) {
                    this.currentState = this.stateMachine.get(PARAMS_NAME_ENUM.BLOCK_BACK)
                } else if (this.params.get(PARAMS_NAME_ENUM.BLOCK_LEFT).value) {
                    this.currentState = this.stateMachine.get(PARAMS_NAME_ENUM.BLOCK_LEFT)
                } else if (this.params.get(PARAMS_NAME_ENUM.BLOCK_RIGHT).value) {
                    this.currentState = this.stateMachine.get(PARAMS_NAME_ENUM.BLOCK_RIGHT)
                } else if (this.params.get(PARAMS_NAME_ENUM.BLOCK_TURN_LEFT).value) {
                    this.currentState = this.stateMachine.get(PARAMS_NAME_ENUM.BLOCK_TURN_LEFT)
                } else if (this.params.get(PARAMS_NAME_ENUM.BLOCK_TURN_RIGHT).value) {
                    this.currentState = this.stateMachine.get(PARAMS_NAME_ENUM.BLOCK_TURN_RIGHT)
                } else if (this.params.get(PARAMS_NAME_ENUM.IDLE).value) {
                    this.currentState = this.stateMachine.get(PARAMS_NAME_ENUM.IDLE)
                } else if (this.params.get(PARAMS_NAME_ENUM.DEATH).value) {
                    this.currentState = this.stateMachine.get(PARAMS_NAME_ENUM.DEATH)
                } else if (this.params.get(PARAMS_NAME_ENUM.AIR_DEATH).value) {
                    this.currentState = this.stateMachine.get(PARAMS_NAME_ENUM.AIR_DEATH)
                } else if (this.params.get(PARAMS_NAME_ENUM.ATTACK).value) {
                    this.currentState = this.stateMachine.get(PARAMS_NAME_ENUM.ATTACK)
                } else {
                    this.currentState = this.currentState
                }
                break;
            default:
                this.currentState = this.stateMachine.get(PARAMS_NAME_ENUM.IDLE)
        }
    }
}


